﻿using System;
using System.Collections.Generic;
using System.Linq;
using BearUtil.Hidden;
using Sirenix.OdinInspector;
using UnityEngine;
using Node = XNode.Node;

namespace BearUtil
{
    [Node.NodeWidthAttribute(350), CreateNodeMenu(menuName: "自定义节点/选项展示"), Title("选项")]
    public class ChoiceNode : BearNode
    {
        [HideInInspector] public int type = 2;

        [Range(1, 3), OnValueChanged("updateLinks")]
        public int ChoiceCount = 2;
        
        [HideLabel, ShowIf("@ChoiceCount >= 1")]
        public ChoiceItem item1;

        [Output, HideLabel, ShowIf("@ChoiceCount >= 1")]
        public BearNode output1;
        [HideLabel, ShowIf("@ChoiceCount >= 2")]
        public ChoiceItem item2;
        [Output, HideLabel, ShowIf("@ChoiceCount >= 2")]
        public BearNode output2;
        [HideLabel, ShowIf("@ChoiceCount >= 3")]
        public ChoiceItem item3;
        [Output, HideLabel, ShowIf("@ChoiceCount >= 3")]
        public BearNode output3;
        
        public string[] GetLines()
        {
            var lines = new List<string>();
            if (!string.IsNullOrEmpty(item1.TextId))
            {
                lines.Add(item1.TextId);
            }
            if (!string.IsNullOrEmpty(item2.TextId))
            {
                lines.Add(item2.TextId);
            }
            if (!string.IsNullOrEmpty(item3.TextId))
            {
                lines.Add(item3.TextId);
            }

            return lines.ToArray();
        }

        /// <summary>
        /// 清理链接
        /// </summary>
        private void updateLinks(int count)
        {
            if(count < 3)
                Outputs.Where(port => port.fieldName.Equals("output3")).First().ClearConnections();
            
            if (count < 2)
                Outputs.Where(port => port.fieldName.Equals("output2")).First().ClearConnections();
        }
    }

    [Serializable]
    public class ChoiceItem
    {
        [LabelText("文本 id"), ValueDropdown("GetLineKey", DropdownWidth = 200), OnValueChanged("switchContent")]
        public string TextId;
        
        // 还没做完，没法显示内容
        [TextArea, ReadOnly, HideLabel] 
        public string Content;

        private List<string> GetLineKey()
        {
            return LanguageCtrl.Instance.Langs;
        }
        
        private void switchContent(string lineKey)
        {
            Content = LanguageCtrl.Instance.GetLineContent(lineKey);
        }
    }
}